package com.migo.api;

import com.migo.annotation.IgnoreAuth;
import com.migo.dto.QueryMyTemplateResultDTO;
import com.migo.entity.*;
import com.migo.service.*;
import com.migo.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Wes on 2018/12/25.
 */
@RestController
@RequestMapping("/api/my/template")
public class ApiMaterialController {

    @Autowired
    private MaterialCategoryService materialCategoryService;


    @Autowired
    private MaterialService materialService;


    @Autowired
    private MyTemplateCategoryService myTemplateCategoryService;


    @Autowired
    private MaterialPublicService materialPublicService;


    @Autowired
    private MyTemplateInteractiveRecordService myTemplateInteractiveRecordService;

    /**
     * 查询素材类型接口
     */
    @IgnoreAuth
    @RequestMapping(value = "listCategory", produces = "application/json;charset=utf-8;")
    public Result listCategory(@RequestParam Map<String, Object> params) throws Exception {
        List<MaterialCategoryEntity> materialCategoryEntities = materialCategoryService.queryListCategory(params);
        Result result = Result.success(materialCategoryEntities);
        result.setHasNext(true);
        return result;
    }

    /**
     * 查询素材接口
     */
    @IgnoreAuth
    @RequestMapping(value = "listMaterial", produces = "application/json;charset=utf-8;")
    public  Result listMaterial(@RequestParam Map<String, Object> params) throws Exception {
        //查询列表数据
        Query query = new Query(params);
        List<MaterialDTOEntity> materialList = materialService.queryMaterialList(query);

        int total = materialService.queryMaterialListTotal(query);
        PageUtils pageUtil = new PageUtils(materialList, total, query.getLimit(), query.getPage());
        Result result = Result.success(materialList);
        result.setHasNext(true);
        return result;
    }

    /**
     * 获取美映剪同款模板分类
     */
    @IgnoreAuth
    @RequestMapping(value = "listCategories", produces = "application/json;charset=utf-8;")
    public R listCategories(@RequestParam Map<String, Object> params) throws Exception {
        List<MyTemplateCategoryEntity> myTemplateCategoryEntities = myTemplateCategoryService.queryList(params);
        Map map =new HashMap<String,List<MyTemplateCategoryEntity>>();
        map.put("categories",myTemplateCategoryEntities);
        R result =R.ok().put("data", map);
        result.put("code",1);
        result.put("enMsg","success");
        result.put("msg","成功");
        return result;
    }

    /*
    *//**
     * 获取美映剪同款模板分类
     *//*
    @IgnoreAuth
    @RequestMapping(value = "listCategories", produces = "application/json;charset=utf-8;")
    public Result listCategories(@RequestParam Map<String, Object> params) throws Exception {
        List<MyTemplateCategoryEntity> myTemplateCategoryEntities = myTemplateCategoryService.queryList(params);
        Map map =new HashMap<String,List<MyTemplateCategoryEntity>>();
        map.put("categories",myTemplateCategoryEntities);

        Result result = new Result(1,"success","成功",JsonUtils. JsonUtils.getJSONString(map), true);

        return result;
    }*/

    /**
     * 获取美映剪同款模板详情
     */
    @IgnoreAuth
    @RequestMapping(value = "detailInfo", produces = "application/json;charset=utf-8;")
    public R detailInfo(@RequestParam Map<String, Object> params) throws Exception {
        QueryMyTemplateResultDTO materialPublicEntity = myTemplateCategoryService.queryMyTemplateDetail(params);
        R result =R.ok().put("data", materialPublicEntity);
        result.put("code",1);
        result.put("enMsg","success");
        result.put("msg","成功");
        return result;
    }

    /**
     * 获取美映剪同款模板详情
     */
    @IgnoreAuth
    @RequestMapping(value = "detailInfoFinal", produces = "application/json;charset=utf-8;")
    public R detailInfoFinal(@RequestParam Map<String, Object> params) throws Exception {
        QueryMyTemplateResultDTO materialPublicEntity = myTemplateCategoryService.queryMyTemplateDetail(params);
        R result =R.ok().put("data", materialPublicEntity);
        result.put("code",1);
        result.put("enMsg","success");
        result.put("msg","成功");
        return result;
    }

    /**
     * 获取美映剪同款模板分页数据
     *  listTemplates返回的packageUrl为zip压缩文件,listTemplatesFinal返回的packageUrl为template文件
     */
    @IgnoreAuth
    @RequestMapping(value = "listTemplates", produces = "application/json;charset=utf-8;")
    public R listTemplates(@RequestParam Map<String, Object> params) throws Exception {
        //查询列表数据
        if(params.containsKey("type")){
            params.put("materialType",params.get("type"));
        }
        Query query = new Query(params);
        List<QueryMyTemplateResultDTO> myTemplateCategoryEntities = myTemplateCategoryService.queryMyTemplateList(query);
        int total = myTemplateCategoryService.queryMyTemplateTotal(query);
        Map map = new HashMap();
        map.put("elements",myTemplateCategoryEntities);
        map.put("total",total);
        R result =R.ok().put("data", map);
        result.put("code",1);
        result.put("enMsg","success");
        result.put("msg","成功");
        return result;
    }

    /**
     * 获取美映剪同款模板分页数据
     *  listTemplates返回的packageUrl为zip压缩文件,listTemplatesFinal返回的packageUrl为template文件
     */
    @IgnoreAuth
    @RequestMapping(value = "listTemplatesFinal", produces = "application/json;charset=utf-8;")
    public R listTemplatesFinal(@RequestParam Map<String, Object> params) throws Exception {
        //查询列表数据
        Query query = new Query(params);
        List<QueryMyTemplateResultDTO> myTemplateCategoryEntities = myTemplateCategoryService.queryMyTemplateList(query);
        int total = myTemplateCategoryService.queryMyTemplateTotal(query);
        PageUtils pageUtil = new PageUtils(myTemplateCategoryEntities, total, query.getLimit(), query.getPage());
        return R.ok().put("data", pageUtil);
    }


    /**
     * 美映剪同款模板互动接口
     */
    @IgnoreAuth
    @RequestMapping(value = "templateInteraction", produces = "application/json;charset=utf-8;")
    public R templateInteraction(@RequestBody  Map<String, String> params) throws Exception {
        String materialId = "";
        R result =R.ok();
        if(params.containsKey("id")){
            materialId = params.get("id");
            MyTemplateInteractiveRecordEntity myTemplateCategoryEntities = myTemplateInteractiveRecordService.queryObject(materialId);
            if (myTemplateCategoryEntities != null) {

                myTemplateCategoryEntities.setUseNum(myTemplateCategoryEntities.getUseNum() + 1);
                myTemplateCategoryEntities.setLikeNum(myTemplateCategoryEntities.getLikeNum() + 1);
                myTemplateInteractiveRecordService.update(myTemplateCategoryEntities);
            } else {
                MyTemplateInteractiveRecordEntity myTemplateInteractiveRecordEntity = new MyTemplateInteractiveRecordEntity();
                myTemplateInteractiveRecordEntity.setMaterialId(materialId);
                myTemplateInteractiveRecordEntity.setLikeNum(0);
                myTemplateInteractiveRecordEntity.setUseNum(1);
                myTemplateInteractiveRecordService.save(myTemplateInteractiveRecordEntity);
            }
            result.put("code",1);
            result.put("enMsg","success");
            result.put("msg","成功");
        }else{

            result.put("code",-1);
            result.put("enMsg","failed,id不能为空");
            result.put("msg","失败");
        }
        return result;
    }
}
